#!/bin/sh

export NOW_DATE=`date "+%Y%m%d"`

#这个是执行命令的用户所在的目录
#APP_DIR=`pwd`
#
#这个是被执行的命令锁在的目录
export APP_DIR=$(cd "$(dirname $0)";pwd)/
echo "APP_DIR: "$APP_DIR

export COMPANY=JiaYuan
export APP_NAME=`grep APP_NAME ${APP_DIR}/release.txt |awk -F ': ' '{print $2}'|sed 's/[[:space:]]//g'`
export APP_VERSION=`grep APP_VERSION ${APP_DIR}/release.txt |awk -F ': ' '{print $2}'|sed 's/[[:space:]]//g'`

export GLOBE_CONF=${APP_DIR}/application.yml

export LOG_FOLDER=${APP_DIR}/log
mkdir -p ${LOG_FOLDER}
export CMD_LOG=${LOG_FOLDER}/start-$APP_NAME-$APP_VERSION-${NOW_DATE}.log
export APP_LOG=${LOG_FOLDER}/run-$APP_NAME-$APP_VERSION.log
LogMsg "APP_LOG: "$APP_LOG

LogMsg()
{
        msg_data=$*
        now_time=`date "+%Y-%m-%d %T"`
        if [ -z "$msg_data" ];then
                msg_data=""
        fi
        echo "\e[0m ${now_time} INFO : $msg_data \e[0m"
        echo "\e[0m ${now_time} INFO : $msg_data \e[0m" >> $CMD_LOG
}
LogSuc()
{
        msg_data=$*
        now_time=`date "+%Y-%m-%d %T"`
        if [ -z "$msg_data" ];then
                msg_data=""
        fi
        echo "\e[32m ${now_time} SUCCESS : $msg_data \e[0m"
        echo "\e[32m ${now_time} SUCCESS : $msg_data \e[0m" >> $CMD_LOG
}
LogWarn()
{
        msg_data=$*
        now_time=`date "+%Y-%m-%d %T"`
        if [ -z "$msg_data" ];then
                msg_data=""
        fi
        echo "\e[33m ${now_time} WARN : $msg_data \e[0m"
        echo "\e[33m ${now_time} WARN : $msg_data \e[0m" >> $CMD_LOG
}
LogErr()
{
        msg_data=$*
        now_time=`date "+%Y-%m-%d %T"`
        if [ -z "$msg_data" ];then
                msg_data=""
        fi
        echo "\e[41m ${now_time} ERROR : $msg_data \e[0m"
        echo "\e[41m ${now_time} ERROR : $msg_data \e[0m" >> $CMD_LOG
}

ShowHelp()
{
        LogWarn "Input error ! You must input one params !"
        LogMsg " sh start.sh runtime"
        LogMsg " sh start.sh server"
        LogMsg " sh start.sh nohup"
        exit 1
}

if [ $# -ne 1 ]; then
        ShowHelp
        exit 1
fi

LogMsg "APP_DIR: "$APP_DIR
export APP_ARGS=$1	
LogMsg "APP_ARGS: "$APP_ARGS

startApp(){
	export JAVA_HOME=/usr/lib/jvm/openjdk-21
	export CLASSPATH=.:${JAVA_HOME}/lib
	export PATH=${JAVA_HOME}/bin:$CLASSPATH:$PATH
	echo "JAVA_HOME: "$JAVA_HOME
	java -version
	
	export LIB_PLUGIN=${APP_DIR}/plugin/
	export APP_PLUGIN=""
	for f in `ls ${LIB_PLUGIN}`
	do
	        export APP_PLUGIN="${LIB_PLUGIN}"$f:$APP_PLUGIN
	done
	echo "APP_PLUGIN:"$APP_PLUGIN

	export LIB_FOLDER=${APP_DIR}/dependency/
	export APP_LIB=""
	for f in `ls ${LIB_FOLDER}`
	do
	        export APP_LIB="${LIB_FOLDER}"$f:$APP_LIB
	done
	echo "APP_LIB:"$APP_LIB


	export APP_JAR=${APP_DIR}/energy-storage-helm-data-0.0.1-SNAPSHOT.jar
	export APP_JAR=${APP_DIR}/helm-storage-data-0.0.1-SNAPSHOT.jar
	export APP_JAR=${APP_DIR}/carbon-access-app-0.0.1-SNAPSHOT.jar
	LogMsg "APP_JAR: "$APP_JAR

	export APP_MAIN=com.jiayuan.cn.energy.EnergyStorageHelmDataApp
	export APP_MAIN=org.springframework.boot.loader.launch.JarLauncher
	LogMsg "APP_MAIN: "$APP_MAIN

	LogMsg "start "$APP_ARGS
	#LogMsg "java -cp ${APP_LIB}:${APP_JAR}:${APP_PLUGIN} $APP_MAIN"

	if [ "$APP_ARGS" = "server" ];then
		sudo systemctl restart missionx.service
	elif [ "$APP_ARGS" = "nohup" ];then
		nohup java -cp ${APP_LIB}:${APP_JAR}:${APP_PLUGIN} $APP_MAIN > ${APP_LOG} 2>&1 &
	else
		#runtime
		java -cp ${APP_LIB}:${APP_JAR}:${APP_PLUGIN} $APP_MAIN
	fi
	LogMsg "see log use command: tail -f log/start-carbon-data-access-0.0.1-*.log log/info.log"

}

startApp

#look flink website
#echo "grep localhost: nohup.out |grep 'granted leadership'"


